package com.wanxi.step07test.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanxi.step07test.dto.ResultDTO;
import com.wanxi.step07test.mapper.UserMapper;
import com.wanxi.step07test.model.UserModel;
import com.wanxi.step07test.service.UserService;
import com.wanxi.step07test.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service(value = "userService")
//@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResultDTO add(UserModel userModel) {
        LambdaQueryWrapper<UserModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(UserModel::getId)
                .eq(userModel.getUsername() != null, UserModel::getUsername, userModel.getUsername());
        boolean exists = userMapper.exists(queryWrapper);
        if (exists) {
            return ResultDTO.builder()
                    .code(0)
                    .msg("have same name")
                    .count(-1)
                    .data("")
                    .build();
        } else {
            String mingwen = userModel.getPassword();
            String anwen = MD5.encoderByMd5(mingwen);
            userModel.setPassword(anwen);

            return ResultDTO.builder()
                    .code(0)
                    .msg("")
                    .count(userMapper.insert(userModel))
                    .data("")
                    .build();
        }
    }

    @Override
    public ResultDTO update(UserModel userModel) {
        LambdaQueryWrapper<UserModel> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.select(UserModel::getPassword)
                .eq(userModel.getUsername() != null, UserModel::getUsername, userModel.getUsername());

        UserModel user = userMapper.selectOne(queryWrapper);
        if (user != null) {

            userModel.setPassword(user.getPassword());

            return ResultDTO.builder()
                    .code(0)
                    .msg("")
                    .count(userMapper.updateById(userModel))
                    .data("")
                    .build();

        } else {
            return ResultDTO.builder()
                    .code(0)
                    .msg("have no")
                    .count(-1)
                    .data("")
                    .build();
        }
    }

    @Override
    public ResultDTO delete(UserModel userModel) {
        return ResultDTO.builder()
                .code(0)
                .msg("")
                .count(userMapper.deleteById(userModel))
                .data("")
                .build();
    }

    @Override
    public ResultDTO findById(UserModel userModel) {


        LambdaQueryWrapper<UserModel> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.select(UserModel::getId, UserModel::getUsername, UserModel::getBirthday,
                        UserModel::getSex, UserModel::getEmail, UserModel::getPhone,
                        UserModel::getAddress, UserModel::getEnable, UserModel::getHobby,
                        UserModel::getVersion,UserModel::getPassword)
                .eq(userModel.getId() != null, UserModel::getId, userModel.getId());

        UserModel user = userMapper.selectOne(queryWrapper);

        return ResultDTO.builder()
                .code(0)
                .msg("")
//                .count(userMapper.count(userModel))
                .data(user)
                .build();
    }

    @Override
    public ResultDTO findAll(UserModel userModel) {

        final String[] hobbies = userModel.getHobby() != null && userModel.getHobby() != "" ? userModel.getHobby().split(",") : null;
//
//        LambdaQueryWrapper<UserModel> queryWrapperCount = new LambdaQueryWrapper<>();
//        queryWrapperCount.select(UserModel::getId)
//                .like(userModel.getUsername() != null && userModel.getUsername() != "", UserModel::getUsername, userModel.getUsername())
//                .eq(userModel.getSex() != null && userModel.getSex() != "", UserModel::getSex, userModel.getSex())
//                .ge(userModel.getStartBirthday() != null && userModel.getStartBirthday() != "", UserModel::getBirthday, userModel.getStartBirthday())
//                .le(userModel.getEndBirthday() != null && userModel.getEndBirthday() != "", UserModel::getBirthday, userModel.getEndBirthday())
//                .like(userModel.getPhone() != null && userModel.getPhone() != "", UserModel::getPhone, userModel.getPhone())
//                .like(userModel.getEmail() != null && userModel.getEmail() != "", UserModel::getEmail, userModel.getEmail())
//                .eq(userModel.getEnable() != null && userModel.getEnable() != -1, UserModel::getEnable, userModel.getEnable())
//                .and(userModel.getHobby() != null && userModel.getHobby() != "", w -> {
//                    for (String hobby : hobbies) {
//                        w.or().like(UserModel::getHobby, hobby);
//                    }
//                });
//
//        Long count = userMapper.selectCount(queryWrapperCount);


        LambdaQueryWrapper<UserModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(UserModel::getId, UserModel::getUsername, UserModel::getBirthday,
                        UserModel::getSex, UserModel::getEmail, UserModel::getPhone,
                        UserModel::getAddress, UserModel::getEnable, UserModel::getHobby)
                .like(userModel.getUsername() != null && userModel.getUsername() != "", UserModel::getUsername, userModel.getUsername())
                .eq(userModel.getSex() != null && userModel.getSex() != "", UserModel::getSex, userModel.getSex())
                .ge(userModel.getStartBirthday() != null && userModel.getStartBirthday() != "", UserModel::getBirthday, userModel.getStartBirthday())
                .le(userModel.getEndBirthday() != null && userModel.getEndBirthday() != "", UserModel::getBirthday, userModel.getEndBirthday())
                .like(userModel.getPhone() != null && userModel.getPhone() != "", UserModel::getPhone, userModel.getPhone())
                .like(userModel.getEmail() != null && userModel.getEmail() != "", UserModel::getEmail, userModel.getEmail())
                .eq(userModel.getEnable() != null && userModel.getEnable() != -1, UserModel::getEnable, userModel.getEnable())
                .and(userModel.getHobby() != null && userModel.getHobby() != "", w -> {
                    for (String hobby : hobbies) {
                        w.or().like(UserModel::getHobby, hobby);
                    }
                })
                .orderByDesc(true,UserModel::getId);;

        IPage<UserModel> page = new Page<>(userModel.getPage(), userModel.getLimit());
        IPage<UserModel> result = userMapper.selectPage(page, queryWrapper);

        return ResultDTO.builder()
                .code(0)
                .msg("success")
                .count((int) result.getTotal())
                .data(result.getRecords())
                .build();
    }


    @Override
    public ResultDTO login(UserModel userModel) {

        if (userModel.getCode().equals(userModel.getVCode())) {
            String mingwen = userModel.getPassword();
            String anwen = MD5.encoderByMd5(mingwen);
            userModel.setPassword(anwen);

            LambdaQueryWrapper<UserModel> queryWrapper = new LambdaQueryWrapper<>();

            queryWrapper.select(UserModel::getUsername, UserModel::getPassword)
                    .eq(userModel.getUsername() != null, UserModel::getUsername, userModel.getUsername())
                    .eq(userModel.getPassword() != null, UserModel::getPassword, userModel.getPassword());

            UserModel user = userMapper.selectOne(queryWrapper);
            if (user != null) {
                return ResultDTO.builder()
                        .code(0)
                        .msg("success")
                        .build();
            } else {
                return ResultDTO.builder()
                        .code(0)
                        .msg("error")
                        .build();
            }
        } else {
            return ResultDTO.builder()
                    .code(0)
                    .msg("error-code")
                    .build();
        }
    }

    @Override
    public ResultDTO password(UserModel userModel) {
        LambdaQueryWrapper<UserModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(UserModel::getId)
                .eq(userModel.getUsername() != null, UserModel::getUsername, userModel.getUsername());
        boolean exists = userMapper.exists(queryWrapper);
        if (exists) {
            String mingwen = userModel.getPassword();
            String anwen = MD5.encoderByMd5(mingwen);
            userModel.setPassword(anwen);

            return ResultDTO.builder()
                    .code(0)
                    .msg("")
                    .count(userMapper.updateById(userModel))
                    .data("")
                    .build();

        } else {
            return ResultDTO.builder()
                    .code(0)
                    .msg("have no")
                    .count(-1)
                    .data("")
                    .build();
        }
    }
}
